package com.noble.glamour.module.logic.baseui.custom;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.v7.widget.AppCompatTextView;
import android.text.Html;
import android.text.TextUtils;
import android.util.AttributeSet;

import com.noble.meether.R;

/**
 * 正计时控件
 * Created by Su on 2017/3/22.
 */

public class CountUpTextView extends AppCompatTextView {

    private OnCountUpListener mListener;
    private CountUpTimer mTimer;
    private String mFormat;         // 自定义时间计时格式
    private long mCountUpTime;      // 计时时间，单位毫秒
    private long mSecond;           // 增加秒数
    private long mMinute;           // 增加分钟
    private long mHour;             // 增加小时
    private String exMinute;        // 展示分钟
    private String exSecond;        // 展示秒数

    public CountUpTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initCountDownTime(context, attrs);
    }

    public CountUpTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initCountDownTime(context, attrs);
    }

    public CountUpTextView(Context context) {
        super(context);
    }

    private void initCountDownTime(Context context, AttributeSet attrs) {
        TypedArray attribute = context.obtainStyledAttributes(attrs, R.styleable.CountDownView);
        mCountUpTime = (long) attribute.getFloat(R.styleable.CountDownView_countDownTime, 0);
        mFormat = attribute.getString(R.styleable.CountDownView_countDownFormat);
        if (TextUtils.isEmpty(mFormat)) {
            mFormat = getContext().getString(R.string.count_default_format);
        }
    }

    public void setCountUpTime(long mCountUpTime) {
        this.mCountUpTime = mCountUpTime;
    }


    private final long h = 60 * 60 * 1000;
    private final long m = 60 * 1000;
    private final long s = 1000;

    public void start() {
        if (mCountUpTime < 0) mCountUpTime = 0;

        if (mCountUpTime == 0) {
            this.setText(Html.fromHtml(String.format(mFormat, "00", "00")));
        }

        if (mTimer != null) {
            mTimer.cancel();
        } else {
            int countUpInterval = 1000;
            mTimer = new CountUpTimer(mCountUpTime, countUpInterval) {

                @Override
                public void onDisplay(long displayMillis) {
                    long t = displayMillis % h;
                    mHour = displayMillis / h;
                    mMinute = t / m;
                    mSecond = (t % m) / s;

                    exFormat(); // 格式化展示时间
                    if (mHour > 0) {
                        mFormat = getContext().getString(R.string.count_rule_format);
                        CountUpTextView.this.setText(Html.fromHtml(String.format(mFormat, mHour, exMinute, exSecond)));
                    } else {
                        mFormat = getContext().getString(R.string.count_default_format);
                        CountUpTextView.this.setText(Html.fromHtml(String.format(mFormat, exMinute, exSecond)));
                    }
                }

                @Override
                public void onTick(long millisPass) {
                    if (mListener != null) {
                        mListener.onTick(mHour, mMinute, mSecond, millisPass);
                    }
                }

                @Override
                public void onFinish(long millisTotal) {
                    long min = millisTotal / (1000 * 60);
                    long sec = (millisTotal % (1000 * 60)) / 1000;
                    long hour = min / 60;
                    if (mListener != null)
                        mListener.onFinish(hour, min, sec, millisTotal);
                }
            };
        }
        mTimer.start();
    }

    /**
     * 计时关闭
     */
    public void cancel() {
        if (mTimer != null)
            mTimer.cancel();
        mTimer = null;
    }

    private void exFormat() {
        exMinute = String.valueOf(mMinute);
        exSecond = String.valueOf(mSecond);

        if (mMinute < 10)
            exMinute = "0" + mMinute;

        if (mSecond < 10)
            exSecond = "0" + mSecond;
    }

    public void setCountUpListener(OnCountUpListener listener) {
        this.mListener = listener;
    }

    public interface OnCountUpListener {
        void onTick(long hour, long min, long sec, long millisPass);   // 计时中

        void onFinish(long hour, long min, long sec, long millisTotal); // 计时完成
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (mTimer != null) mTimer.cancel();
        mTimer = null;
    }
}
